package com.lgh.demo.activity;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;
import com.lgh.demo.R;
import com.lgh.demo.bean.BeanPayResult;
import com.lgh.demo.databinding.ActivityPayBinding;
import com.lgh.demo.util.ApiUtils;
import com.lgh.demo.util.Appconfig;
import com.lgh.demo.util.NetUtils;
import com.lgh.demo.util.WMConstantUtil;
import com.lgh.demo.util.ZXingUtils;
import com.lgh.demo.view.BorderView;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

public class PayActivity extends BaseActivity {

    private String orderID;
    private ActivityPayBinding activityPayBinding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityPayBinding = DataBindingUtil.setContentView(this, R.layout.activity_pay);
        String payURL = getIntent().getStringExtra(WMConstantUtil.KEY_cashier_url);
        orderID = getIntent().getStringExtra(WMConstantUtil.KEY_Order_ID);
        String sucessURL =  getIntent().getStringExtra(WMConstantUtil.KEY_pay_sucess_url);
        String failURL = ApiUtils.Pay_Fail_URL;

        String payUrlWithOrderNotify = "";
        try {
            payUrlWithOrderNotify = payURL + "&pay_success_url=" + URLEncoder.encode(sucessURL, "UTF-8")
                    + "&redr_url=" + URLEncoder.encode(failURL, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        Bitmap codeBitmap = ZXingUtils.createQRImage(payUrlWithOrderNotify, 500, 500);
        activityPayBinding.ivCode.setImageBitmap(codeBitmap);

        BorderView borderView = new BorderView(this);
        borderView.setBackgroundResource(R.drawable.focus_default);
        borderView.attachTo(activityPayBinding.rlOperate);
    }

    public void queryResult(View view) {
        String path = ApiUtils.getMyUrl(ApiUtils.URL_orderCheck + orderID + "&CANO=" + Appconfig.getKeyNo(this));
        NetUtils.getData(this, path, null, BeanPayResult.class, new NetUtils.NetCallBack() {
            @Override
            public void netBack(int requestTag, Object object) {
                if (object != null) {
                    BeanPayResult beanPayResult = (BeanPayResult) object;
                    if (beanPayResult.getCode()==0 && !TextUtils.isEmpty(beanPayResult.getResult().getSn())){
                        Toast.makeText(PayActivity.this, R.string.pay_sucess_tip, Toast.LENGTH_LONG).show();
                        goHome(activityPayBinding.tvGoHome);
                    }else{
                        Toast.makeText(PayActivity.this, R.string.pay_fail_tip, Toast.LENGTH_LONG).show();
                    }
                }
            }
        });
    }

    public void goHome(View view) {
        Intent intent = new Intent(this, HomeActivity.class);
        startActivity(intent);
    }

}
